<?php
class Provider extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="provider/index_view";
        $this->_data["titlePage"]="Quản lý nhà cung cấp";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_name");
        $this->session->unset_userdata("sess_search_address");
        $this->session->unset_userdata("sess_search_phone");
        $this->session->unset_userdata("sess_search_email");
        $this->session->unset_userdata("sess_search_area");
        
        $this->load->model("mprovider");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/provider/index/",
            "total_rows"=>$this->mprovider->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mprovider->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mprovider->listAllLimit($config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function add() {
        $name=$this->input->post("name");
        $address=$this->input->post("address");
        $phone=$this->input->post("phone");
        $email=$this->input->post("email");
        $area=$this->input->post("area");
        $note=$this->input->post("note");
        $this->load->model("mprovider");
        if($this->mprovider->checkName($name) == false)
            echo "name";
        else if($this->mprovider->checkEmail($email) == false)
            echo "email";
        else {
            $data=array(
                "prov_name"=>$name,
                "prov_address"=>$address,
                "prov_phone"=>$phone,
                "prov_email"=>$email,
                "prov_area"=>$area,
                "prov_note"=>$note,
            );
            $this->mprovider->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm nhà cung cấp thành công.");
            echo "Thêm nhà cung cấp thành công!";
        }
    }
    
    public function edit() {
        $this->_data["loadPage"]="provider/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa nhà cung cấp";
        
        //load data
        $id=$this->uri->segment(4);
        $this->load->model("mprovider");
        $temp=$this->mprovider->getById($id);
        if($temp!=false)
            $this->_data["info_edit"]=$temp;
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtName",
                "label"=>"Tên",
                "rules"=>"callback_checkName"
            ),
            array(
                "field"=>"txtEmail",
                "label"=>"Email",
                "rules"=>"callback_checkEmail"
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "prov_name"=>$this->input->post("txtName"),
                "prov_address"=>$this->input->post("txtAddress"),
                "prov_phone"=>$this->input->post("txtPhone"),
                "prov_email"=>$this->input->post("txtEmail"),
                "prov_area"=>$this->input->post("txtArea"),
                "prov_note"=>$this->input->post("txtNote"),
            );
            $this->mprovider->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa user thành công.");
            redirect(base_url()."admin_combo/provider/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("mprovider");
        $this->mprovider->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa nhà cung cấp thành công.");
        redirect(base_url()."admin_combo/provider/");
    }
    
    public function search() {
        $this->_data["loadPage"]="provider/search_view";
        $this->_data["titlePage"]="Tìm kiếm nhà cung cấp";
        
        $name=$this->session->userdata("sess_search_name");
        $address=$this->session->userdata("sess_search_address");
        $phone=$this->session->userdata("sess_search_phone");
        $email=$this->session->userdata("sess_search_email");
        $area=$this->session->userdata("sess_search_area");
        
        $this->load->model("mprovider");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/provider/search/",
            "total_rows"=>$this->mprovider->countAllResults($name, $address, $phone, $email, $area)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mprovider->search($name, $address, $phone, $email, $area, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mprovider->search($name, $address, $phone, $email, $area, $config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchProvider"])) {
            $sess=array(
                "sess_search_name"=>$this->input->post("txtName"),
                "sess_search_address"=>$this->input->post("txtAddress"),
                "sess_search_phone"=>$this->input->post("txtPhone"),
                "sess_search_email"=>$this->input->post("txtEmail"),
                "sess_search_area"=>$this->input->post("txtArea")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/provider/search");
        }
    }
    
    public function reloadData() {
        $this->load->model("mprovider");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/provider/index",
            "total_rows"=>$this->mprovider->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mprovider->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mprovider->listAllLimit($config["per_page"], $start);
        $this->load->view("provider/reload_view", $this->_data);
    }
    
    public function checkName($name) {
        $this->load->model("mprovider");
        $id=$this->uri->segment(4);
        if(!$this->mprovider->checkName($name, $id)) {
            $this->form_validation->set_message("checkName", "Tên này đã được đăng ký!");
            return false;
        }else
            return true;
    }
    
    public function checkEmail($email) {
        $this->load->model("mprovider");
        $id=$this->uri->segment(4);
        if(!$this->mprovider->checkEmail($email, $id)) {
            $this->form_validation->set_message("checkEmail", "Email này đã được đăng ký!");
            return false;
        }else
            return true;
    }
}
?>
